package in.muton.edushare.auth.roles;

import java.util.List;

import com.edushare.common.vo.Departments;
import com.edushare.common.vo.UnivDeptLink;
import com.edushare.common.vo.UserDeptLink;
import com.edushare.common.vo.Users;

public class DeptRoleCheck {

	public DeptRoleCheck(Departments dept, Users user) {
		this.dept = dept;
		this.u = user;
	}

	public static int getRole(Departments dept, Users user) {
		List<UserDeptLink> udllist = user.getDepartmentLinks();
		for (UserDeptLink u : udllist) {
			if (u.getDepartmentLink() == dept
					&& u.getStatus() == UserDeptLink.STATUS_ACTIVE) {
				return u.getRoleType();
			}
		}
		return 0;
	}

	public boolean isMember() {
		return getRole(dept, u) != 0;
	}

	public boolean isPrimaryAdmin() {
		return isPrimaryAdmin(dept, u);
	}

	public boolean isSecondaryAdmin() {
		return isSecondaryAdmin(dept, u);
	}

	public boolean isStudent() {
		return isStudent(dept, u);
	}

	public boolean isTeacher() {
		return isTeacher(dept, u);
	}

	public boolean isUnivAdmin() {
		return isUnivAdmin(dept, u);
	}
	
	public boolean isAdmin(){
		return isUnivAdmin()|| isPrimaryAdmin();
	}

	public static boolean isAdmin(Departments dept, Users user){
		return isPrimaryAdmin(dept, user)||isUnivAdmin(dept, user);
	}
	
	public static boolean isPrimaryAdmin(Departments dept, Users user) {
		return getRole(dept, user) == UserDeptLink.PRIMARY_ADMIN;
	}

	public static boolean isSecondaryAdmin(Departments dept, Users user) {
		return getRole(dept, user) == UserDeptLink.SECONDARY_ADMIN;
	}

	public static boolean isTeacher(Departments dept, Users user) {
		return getRole(dept, user) == UserDeptLink.TEACHER;
	}

	public static boolean isStudent(Departments dept, Users user) {
		return getRole(dept, user) == UserDeptLink.STUDENT;
	}
	
	public static boolean isUnivMember(Departments dept, Users user){
		List<UnivDeptLink> udl = dept.getUnivLinks();
		for (UnivDeptLink u : udl) {
			if (u.getAssociationType() == UnivDeptLink.UNIVERSITY_DEPT_OWNER) {
				if(UnivRoleCheck.getRole(u.getUniversity(), user) != 0)
					return true;
			}
		}
		return false;
	}

	public static boolean isUnivAdmin(Departments dept, Users user) {
		List<UnivDeptLink> udl = dept.getUnivLinks();
		for (UnivDeptLink u : udl) {
			if (u.getAssociationType() == UnivDeptLink.UNIVERSITY_DEPT_OWNER) {
				if(UnivRoleCheck.isAdmin(u.getUniversity(), user))
					return true;
			}
		}
		return false;
	}
	
	
	public int getRole(){
		return getRole(dept, u);
	}

	private Departments dept;
	private Users u;
}
